package com.jiyi.activity.datainput.jm;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.jiyi.activity.datainput.sm.SignRecordActivity;
import com.jiyi.common.StringInterface;
import com.jiyi.common.VolleyRequest;
import com.jiyi.easemob.util.UserHelp;
import com.jiyi.inteface.WebApi;
import com.swift.base.ActivityBase;
import com.swift.maintenance.R;
import com.swift.maintenance.base.AppBase;
import com.swift.maintenance.base.AppConfig;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.maintenance.utils.UserInfoUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/3/30.
 */

public class JMAddMainActivity extends ActivityBase {
    /* 头部 */
    @BindView(R.id.tvBack)
    TextView tvBack;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvSearch)
    TextView tvSearch;
    @BindView(R.id.tvSignCount)
    TextView tvSignCount;
    @BindView(R.id.tvSign)
    TextView tvSign;
    @BindView(R.id.tvSignRecord)
    TextView tvSignRecord;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sm_add_main);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        setTop();
        getData();
    }

    private void setTop() {
        tvTitle.setText("高层建筑消防安全评估系统");
        tvSearch.setVisibility(View.GONE);
    }

    @OnClick({ R.id.tvAdd, R.id.tvView,R.id.tvSign,R.id.tvSignRecord,R.id.tvBack })
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.tvBack:
                finish();
                break;
            case R.id.tvAdd:
                intent = new Intent(this, JMDataInputActivity.class);
                intent.putExtra("type", 2);
                startActivity(intent);
                break;
            case R.id.tvView:
                intent = new Intent(this, JMDataInputListActivity.class);
                startActivity(intent);
                break;
            case R.id.tvSign:
                sign();
                break;
            case R.id.tvSignRecord:
                intent = new Intent(this, SignRecordActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    private void getData() {
        Map<String, String> params = new HashMap<String, String>();
        params.put(AppConfig.ACCESS_TOKEN,
                UserInfoUtil.getCurrentUserAccessToken());

        VolleyRequest.StringRequestPost(this,
                WebApi.DATA_INPUT_SIGN_PAGE, "", params, new StringInterface(
                        this) {

                    @Override
                    public void stringSuccess(String result) {
                        try {
                            JSONObject obj = new JSONObject(result);
                            String status = obj.getString("status");
                            String info = obj.getString("info");
                            if (status.equals("200")) {
                                String data = obj.getString("data");
                                String count = new JSONObject(data).getString("count");
                                String is_sign = new JSONObject(data).getString("is_sign");
                                tvSignCount.setText(count);
                                if (is_sign.equals("0")) {
                                    //未签到
                                    tvSign.setText("今日签到");
                                    tvSign.setEnabled(true);
                                }
                                else {
                                    //已签到
                                    tvSign.setEnabled(false);
                                    tvSign.setText("已签到");
                                    tvSign.setTextColor(Color.parseColor("#666666"));
                                }
                            }
                            else if (status.equals("404") && info.contains("尚未登录")) {
                                ToastUtil.getInstances().showMsg("尚未登录或该用户在其他地方登录，请重新登录！");
                                //清除环信登录
                                UserHelp.getInstance().clearUser(AppBase.Context, true);
                                return;
                            }
                            else {
                                ToastUtil.getInstances().showMsg(info);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        } finally {

                        }
                    }

                    @Override
                    public void stringError(VolleyError error) {

                    }
                });
    }

    private void sign() {
        Map<String, String> params = new HashMap<String, String>();
        params.put(AppConfig.ACCESS_TOKEN,
                UserInfoUtil.getCurrentUserAccessToken());

        VolleyRequest.StringRequestPost(this,
                WebApi.DATA_INPUT_SIGN, "", params, new StringInterface(
                        this) {

                    @Override
                    public void stringSuccess(String result) {
                        try {
                            JSONObject obj = new JSONObject(result);
                            String status = obj.getString("status");
                            String info = obj.getString("info");
                            if (status.equals("200")) {
                                ToastUtil.getInstances().showMsg("签到成功");
                                tvSign.setEnabled(false);
                                tvSign.setText("已签到");
                                tvSign.setTextColor(Color.parseColor("#666666"));
                                int signCount = Integer.parseInt(tvSignCount.getText().toString())+1;
                                tvSignCount.setText(signCount+"");
                            }
                            else if (status.equals("404") && info.contains("尚未登录")) {
                                ToastUtil.getInstances().showMsg("尚未登录或该用户在其他地方登录，请重新登录！");
                                //清除环信登录
                                UserHelp.getInstance().clearUser(AppBase.Context, true);
                                return;
                            }
                            else {
                                ToastUtil.getInstances().showMsg(info);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        } finally {

                        }
                    }

                    @Override
                    public void stringError(VolleyError error) {

                    }
                });
    }
}
